package com.client.koshercook.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;

/** a widget used for displaying and changing cook time of a recipe */
public class CookTime extends Composite{
	
	private HorizontalPanel addTime = new HorizontalPanel();
	private VerticalPanel panel = new VerticalPanel();
	private Label cookTimeHeader = new Label();
	private String link = null;
	private ListBox userMinutes = new ListBox();
	private ListBox userHours = new ListBox();
	private VerticalPanel userMinutesPanel = new VerticalPanel();
	private VerticalPanel userHoursPanel = new VerticalPanel();
	private PushButton addButton = new PushButton();
	
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
	
	/** create a new CookTime. use setRecipe to associate it with a recipe */
	public CookTime() {

		//init the list-boxes of hours and minutes
		for(int i=0; i<60; i++)
			userMinutes.addItem(Integer.toString(i));
		for(int i=0; i<24; i++)
			userHours.addItem(Integer.toString(i));
		userMinutes.setSelectedIndex(0);
		userHours.setSelectedIndex(0);
		userMinutesPanel.add(userMinutes);
		userMinutesPanel.add(new Label("דקות"));
		userHoursPanel.add(userHours);
		userHoursPanel.add(new Label("שעות"));
		
		addButton.setText("הוסף זמן הכנה");
		addButton.setTitle("לחץ כדי להוסיף את זמן ההכנה הידוע לך");
		addButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(!Client.model.isConnected()){
					Client.model.setInfoTextAndPO("עליך להיות מחובר בכדי להוסיף זמן הכנה למתכון");
					return;
				}
				else{
					String minutesText = userMinutes.getItemText(userMinutes.getSelectedIndex());
					String hoursText = userHours.getItemText(userHours.getSelectedIndex());
					int newTime = Integer.parseInt(hoursText)*60 + Integer.parseInt(minutesText);
					
					// check that user has selected a time
					if(newTime == 0){
						Client.model.setInfoTextAndPO("יש לבחור זמן הכנה כלשהו");
						return;
					}	
					// get the updated cook time from the server, including this new time from the user
					getNewCookTime(link, newTime);

					//disable the buttons
					userMinutes.setEnabled(false);
					userHours.setEnabled(false);
					addButton.setEnabled(false);										
				}
			}
		});
		
		cookTimeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		cookTimeHeader.addStyleName("infoTextHeader");
		panel.setSpacing(5);
		addTime.setSpacing(3);
		addTime.add(addButton);
		addTime.add(userHoursPanel);
		addTime.add(userMinutesPanel);
		panel.add(cookTimeHeader);
		panel.add(addTime);
		panel.setCellHorizontalAlignment(addTime, HasHorizontalAlignment.ALIGN_CENTER);
		panel.setCellHorizontalAlignment(cookTimeHeader, HasHorizontalAlignment.ALIGN_CENTER);
		
		initWidget(panel);
	}
	
	/** associate the cook time with a recipe, and show it's current time */
	public void setRecipe(String link, int initialGrade){
		this.link = link;
		updateCookTimeDisplay(initialGrade);
	}
	
	/** get the new Cook Time of recipe recipeName from the server */
	private void getNewCookTime(String recipeName, int newGrade) { 
		greetingService.getNewCookTime(recipeName, newGrade, new AsyncCallback<Integer>(){
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(Integer result) {
				updateCookTimeDisplay(result);	//setting the new image for the rater
				if(result>0)
					Client.model.setInfoTextAndPO("זמן הכנה נקלט בהצלחה");
				else if(result==0)
					Client.model.setInfoTextAndPO("עליך להיות מחובר כדי להכניס זמן הכנה");
				else if(result<0)
					Client.model.setInfoTextAndPO("כבר הכנסת זמן הכנה למתכון זה בעבר");
			}
		});
	}

	/** update the display of a new cook time */ 
	private void updateCookTimeDisplay(int time){
		if(time<0)//grade error - keep the old grade
			return;
		if(time==0){
			cookTimeHeader.setText("היה הראשון להכניס זמן הכנה");
			return;
		}
		int hours = time / 60;
		int minutes = time % 60;
		switch (hours){
			case 0:
				cookTimeHeader.setText("זמן הכנה ממוצע: "  + minutes + " דקות");
				break;
			case 1:
				cookTimeHeader.setText("זמן הכנה ממוצע: " + " שעה ו- " +  minutes + " דקות");
				break;
			case 2:
				cookTimeHeader.setText("זמן הכנה ממוצע: " + " שעתיים ו- " +  minutes + " דקות");
				break;
			default:
				cookTimeHeader.setText("זמן הכנה ממוצע: " + hours + " שעות ו- " +  minutes + " דקות");
		}
		return;
	}
	
	/* use this to disable this widget */
	public void disable(){
		addButton.setEnabled(false);
		userHours.setEnabled(false);
		userMinutes.setEnabled(false);
		userMinutes.setSelectedIndex(0);
		userHours.setSelectedIndex(0);
	}
	
	/* use this to enable this widget */
	public void enable(){
		userMinutes.setSelectedIndex(0);
		userHours.setSelectedIndex(0);
		if(Client.model.isConnected()){
			addButton.setEnabled(true);
			userHours.setEnabled(true);
			userMinutes.setEnabled(true);
		}
		else
			disable();	
	}
	
	/* use this to clear all text fields */
	public void clearText(){
		cookTimeHeader.setText("");
		userMinutes.setSelectedIndex(0);
		userHours.setSelectedIndex(0);
	}
}